/**
 * created by dqn 2018/3/7
 * 搜索api
 */

package main.controller;

import main.entity.Song;
import main.service.SongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@Controller
@RestController
public class SearchController {
    @Autowired
    SongService songService;

    /**
     * 关键字搜索
     * @param key
     * @param httpSession
     * @return json [{song1},{song2}..]
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public Map<String,List<Song>> seachSongContains(String key, HttpSession httpSession) {
        try {
            key = new String(key.getBytes("iso-8859-1"),"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        httpSession.setAttribute("searchKey",key);
        return songService.getSongsByContains(key);
    }


    /**
     * 返回正在搜索的key
     * @param httpSession
     * @return
     */
    @RequestMapping(value = "/search/key",method = RequestMethod.GET)
    public Map<String,Object> getCurrentSearchKey(HttpSession httpSession){
        Map<String,Object> map = new HashMap<>();
        String key = (String) httpSession.getAttribute("searchKey");
        System.out.println(key);
        map.put("key",key);
        return map;
    }
}
